<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2019/12/27
 * Time: 9:55
 */
namespace App\Common\Utils;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class BaseRepository
{
    protected $model;


    /**
     * 获取单条数据
     * @param $id
     * @param string $keyName
     * @param string[] $columns
     * @param bool $whereRaw
     * @return Collection
     */
    public function get($id, $keyName = 'id', $columns = ['*'], $whereRaw = FALSE){
        if ( $keyName === FALSE ) {
            $where = $id;
        } else {
            $where = [
                $keyName => $id,
            ];
        }
        if(!$whereRaw){
            return $this->model->where($where)->get($columns)->first();
        }else{
            return $this->model->where($where)->whereRaw($whereRaw)->get($columns)->first();
        }
    }

    /**
     * 获取一个字段
     * @param array $where
     * @param string $field
     */
    public function getValue($where,$field){
        return $this->model->where($where)->value($field);
    }

    /**
     * 获取统计数
     */
    public function getCount($where){
        return $this->model->where($where)->count();
    }


    /**
     * 获取分页数据
     * @param array $filters
     * @param array $sorts
     * @param int $page
     * @param string[] $columns
     * @param int $pageSize
     * @param string $pageName
     * @return mixed
     */
    public function getList($filters=[], $sorts =[], $page=1, $columns=['*'], $pageSize=10, $pageName='page'){
        $result = $this->model;
        if(!empty($sorts)){
            foreach ($sorts as $key=>$val){
                $result = $result->orderBy($key, $val);
            }
        }
        return $result->where($filters)->paginate($pageSize,$columns,$pageName,$page);
    }

    public function getListOr($filters=[],$orwhere=[], $sorts =[], $page=1, $columns=['*'], $pageSize=10, $pageName='page'){
        $result = $this->model;
        if(!empty($sorts)){
            foreach ($sorts as $key=>$val){
                $result = $result->orderBy($key, $val);
            }
        }
        return $result->where($filters)->orWhere($orwhere)->paginate($pageSize,$columns,$pageName,$page);
    }


    // 获取所有
    public function getAll($filters=[], $sorts =[], $columns=['*'],$whereRaw = FALSE){
        $result = $this->model;
        if(!empty($sorts)){
            foreach ($sorts as $key=>$val){
                $result = $result->orderBy($key, $val);
            }
        }
        if(!$whereRaw){
            return $result->where($filters)->get($columns);
        }else{
            return $result->where($filters)->whereRaw($whereRaw)->get($columns);
        }
    }


    /**
     * 添加
     * @param $data
     * @return int
     */
    public function add($data, $backAll = FALSE){
        $recode = $this->model->create($data);
        
        if($backAll) {
            return $recode;
        }

        return $recode->id;
    }


    /**
     * 批量添加
     * @param array $data
     */
    public function addAll(array $data){
        $this->model->insert($data);
    }


    /**
     * 编辑
     * @param $data
     * @param $id
     * @param string $keyName
     * @param bool $whereRaw
     * @return bool
     */
    public function edit($data, $id , $keyName = 'id', $whereRaw = FALSE){
        if ( $keyName === FALSE ) {
            $where = $id;
        } else {
            $where = [
                $keyName => $id,
            ];
        }
        if(!$whereRaw){
            return $this->model->where($where)->update($data);
        }else{
            return $this->model->where($where)->whereRaw($whereRaw)->update($data);
        }
    }

    /**
     * 新增或者修改
     * @param array $where
     * @param array $data
     * @return bool
     */
    public function saveOrInsert($where=[],$data=[])
    {
        if(empty($where) || empty($data)){
            return false;
        }
        return $this->model->updateOrInsert($where,$data);
    }

    /**
     * 更新或者插入 (返回模型)
     * @param $where
     * @param $data array
     * @return bool
     */
    public function saveOrCreate($where,$data)
    {
        if(empty($where) || empty($data)){
            return false;
        }
        return $this->model->updateOrCreate($where,$data);
    }


    /**
     * 删除
     * @param $id
     * @param string $keyName
     */
    public function delete($id , $keyName = 'id'){
        if ( $keyName === FALSE ) {
            $where = $id;
        } else {
            $where = [
                $keyName => $id,
            ];
        }
        return $this->model->where($where)->delete();
    }


    /**
     * 字段自增
     * @param array $where
     * @param string $field
     * @param int $sum
     * @return bool
     */
    public function increment($where=[], $field='view_num', $sum=1)
    {
        if(empty($where)) return false;
        return $this->model->where($where)->increment($field,$sum);
    }


    /**
     * 字段自减
     * @param array $where
     * @param string $field
     * @param int $sum
     * @return bool
     */
    public function decrement($where=[], $field='view_num', $sum=1)
    {
        if(empty($where)) return false;
        return $this->model->where($where)->decrement($field,$sum);
    }

    /**
     * 原生sql查询
     * @param $sql
     * @return array
     */
    public function runQrySql($sql){
        return DB::select($sql);
    }

    public function count($where=[])
    {
        return $this->model->where($where)->count();
    }
}
